package com.cskaoyan.service.wx.lghc;

import com.cskaoyan.mapper.*;
import com.cskaoyan.module.bean.vo.VxShopCartCheckoutVO;
import com.cskaoyan.module.bean.vo.VxShopCartIndexVO;
import com.cskaoyan.module.bean.vo.VxShopCartInnerIndexVO;
import com.cskaoyan.module.bo.VxShopCartAddBO;
import com.cskaoyan.module.bo.VxShopCartFastAddBO;
import com.cskaoyan.module.bo.VxShopCartUpdateBO;
import com.cskaoyan.module.bo.VxShopcartCheckedOutBO;
import com.cskaoyan.module.pojo.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author :ligenghuisheng
 * @Description:
 * @Date :2022/4/9  15:42
 */
@Service
public class VxShopCartServiceImpl implements VxShopCartService {

    @Autowired
    MarketCartMapper marketCartMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;

    @Autowired
    MarketOrderMapper marketOrderMapper;

    @Autowired
    MarketAddressMapper marketAddressMapper;

    @Autowired
    MarketCouponUserMapper couponUserMapper;

    @Autowired
    MarketCouponMapper marketCouponMapper;


    @Override
    public VxShopCartIndexVO indexShopCart(VxShopCartIndexVO vxShopCartIndexVO, MarketCart marketCart, VxShopCartInnerIndexVO vxShopCartInnerIndexVO,Integer id) {
        MarketCartExample marketCartExample = new MarketCartExample();
        List<MarketCart> marketCarts = marketCartMapper.selectByExample(marketCartExample);
        VxShopCartIndexVO vxShopCartIndexVO1 = new VxShopCartIndexVO();
       Integer[] resultCheck= marketCartMapper.selectByUserIdChecked(id);
        if(resultCheck.length==0 ) {
            //查询这个购物车所有物品的总量
           // int result = marketCartMapper.selectCountNumber();
            int result =0;
            vxShopCartInnerIndexVO.setGoodsCount(result);
            //查询这个购物车所有物品价格总和
            //double result2 = marketCartMapper.selectCountAmount();
            double result2 = 0.0;
            vxShopCartInnerIndexVO.setGoodsAmount(result2);
            //没有被选中所以checkedcount和checkamount为0
            Double result3 = 0.0;
            Integer result4 = 0;
            vxShopCartInnerIndexVO.setCheckedGoodsAmount(result3);
            vxShopCartInnerIndexVO.setCheckedGoodsCount(result4);


            vxShopCartIndexVO1.setCartTotal(vxShopCartInnerIndexVO);
            vxShopCartIndexVO1.setCartList(marketCarts);
        }else {
            int result = marketCartMapper.selectCountNumber();
            vxShopCartInnerIndexVO.setGoodsCount(result);
            //查询这个购物车所有物品价格总和
            double result2 = marketCartMapper.selectCountAmount();
            vxShopCartInnerIndexVO.setGoodsAmount(result2);

          Double checAmount =  marketCartMapper.selectPrice();
          Integer cheackCount = marketCartMapper.selectNumber();
          vxShopCartInnerIndexVO.setCheckedGoodsAmount(checAmount);
          vxShopCartInnerIndexVO.setCheckedGoodsCount(cheackCount);

            vxShopCartIndexVO1.setCartTotal(vxShopCartInnerIndexVO);
            vxShopCartIndexVO1.setCartList(marketCarts);

        }



        return vxShopCartIndexVO1;
    }

    @Override
    public int goodsCount() {

        int result = marketCartMapper.selectCountNumber();
        System.out.println("result = " + result);

        return result;
    }

    @Override
    public VxShopCartIndexVO goodsChech(List<Integer> productIds, Integer isChecked) {
        VxShopCartInnerIndexVO vxShopCartInnerIndexVO = new VxShopCartInnerIndexVO();


        VxShopCartIndexVO vxShopCartIndexVO1 = new VxShopCartIndexVO();
        if(isChecked==1){
            //更新这个选中的状态
            marketCartMapper.updateChecked(productIds);


            //查询这个购物车被选中的物品个数

           Integer result5 =  marketCartMapper.selectCheckedCount();
            //查询这个购物车被选中的物品总价格
           Double result6 = marketCartMapper.selectCheckedAmount();

           vxShopCartInnerIndexVO.setCheckedGoodsCount(result5);
           vxShopCartInnerIndexVO.setCheckedGoodsAmount(result6);
            //查询这个购物车所有物品的总量
            int result = marketCartMapper.selectCountNumber();

            vxShopCartInnerIndexVO.setGoodsCount(result);
            //查询这个购物车所有物品价格总和
            double result2 = marketCartMapper.selectCountAmount();
            vxShopCartInnerIndexVO.setGoodsAmount(result2);

            MarketCartExample marketCartExample = new MarketCartExample();
            List<MarketCart> marketCarts = marketCartMapper.selectByExample(marketCartExample);

            vxShopCartIndexVO1.setCartList(marketCarts);
            vxShopCartIndexVO1.setCartTotal(vxShopCartInnerIndexVO);



        }else {
            Integer[] checked = marketCartMapper.selectByProductIdCheck();
            marketCartMapper.updateCheckedBack(productIds);

            if (checked.length == 0) {

                Double result3 = 0.0;
                Integer result4 = 0;
                vxShopCartInnerIndexVO.setCheckedGoodsAmount(result3);
                vxShopCartInnerIndexVO.setCheckedGoodsCount(result4);
                int result = 0;

                vxShopCartInnerIndexVO.setGoodsCount(result);
                //查询这个购物车所有物品价格总和
                double result2 = 0.0;
                vxShopCartInnerIndexVO.setGoodsAmount(result2);

            }else {




                    Double resultAount = marketCartMapper.selectByProductIdAmount();
                    Integer resultCount = marketCartMapper.selectBYProductCount();
                    vxShopCartInnerIndexVO.setCheckedGoodsAmount(resultAount);
                    vxShopCartInnerIndexVO.setCheckedGoodsCount(resultCount);
                    //查询这个购物车所有物品的总量
                    int result = marketCartMapper.selectCountNumber();

                    vxShopCartInnerIndexVO.setGoodsCount(result);
                    //查询这个购物车所有物品价格总和
                    double result2 = marketCartMapper.selectCountAmount();
                    vxShopCartInnerIndexVO.setGoodsAmount(result2);

            }
            MarketCartExample marketCartExample = new MarketCartExample();
            List<MarketCart> marketCarts = marketCartMapper.selectByExample(marketCartExample);


            vxShopCartIndexVO1.setCartList(marketCarts);
            vxShopCartIndexVO1.setCartTotal(vxShopCartInnerIndexVO);

        }

        return vxShopCartIndexVO1;
    }

    @Override
    public void goodsUpdate(VxShopCartUpdateBO shopCartUpdateBO) {
        Integer number = shopCartUpdateBO.getNumber();

        Integer id = shopCartUpdateBO.getId();
        marketCartMapper.updateById(number,id);

    }

    @Override
    public int shopCartAdd(VxShopCartAddBO vxShopCartAddBO,Integer id) {

        int result =  marketCartMapper.selectTotalNumber(vxShopCartAddBO.getProductId());
        int resultTotal = 0;
        if(result!=0){
           marketCartMapper.updateEixtGoods(vxShopCartAddBO.getProductId(),vxShopCartAddBO.getNumber());
           //查询这个购物车所有物品的总量
            resultTotal  = marketCartMapper.selectCountNumber();
       }else {


            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(vxShopCartAddBO.getGoodsId());
            String goodsSn = marketGoods.getGoodsSn();
            String name = marketGoods.getName();
            String picUrl = marketGoods.getPicUrl();

            MarketGoodsProduct marketGoodsProduct = marketGoodsProductMapper.selectByPrimaryKey(vxShopCartAddBO.getProductId());
            BigDecimal price = marketGoodsProduct.getPrice();
            String[] specifications = marketGoodsProduct.getSpecifications();


            marketCartMapper.insertNoEistData(id,vxShopCartAddBO.getGoodsId(),goodsSn,name,vxShopCartAddBO.getProductId(),price,vxShopCartAddBO.getNumber(),specifications,picUrl);

            //查询这个购物车所有物品的总量
            resultTotal  = marketCartMapper.selectCountNumber();
        }



        return resultTotal;
    }

    @Override
    public VxShopCartIndexVO deleteShopCart(List<Integer> productIds) {
      int result = marketCartMapper.deleteByProdeuctId(productIds);
        MarketCartExample example = new MarketCartExample();
        VxShopCartIndexVO vxShopCartIndexVO = new VxShopCartIndexVO();
        VxShopCartInnerIndexVO vxShopCartInnerIndexVO = new VxShopCartInnerIndexVO();
      if(result!=0){

          //查询这个购物车所有物品的总量
          int result1 = marketCartMapper.selectCountNumber();

          vxShopCartInnerIndexVO.setGoodsCount(result1);
          //查询这个购物车所有物品价格总和
          double result2 = marketCartMapper.selectCountAmount();
          vxShopCartInnerIndexVO.setGoodsAmount(result2);

          Double result3 = 0.0;
          Integer result4 = 0;
          vxShopCartInnerIndexVO.setCheckedGoodsAmount(result3);
          vxShopCartInnerIndexVO.setCheckedGoodsCount(result4);

          List<MarketCart> marketCarts = marketCartMapper.selectByExample(example);
          vxShopCartIndexVO.setCartTotal(vxShopCartInnerIndexVO);
          vxShopCartIndexVO.setCartList(marketCarts);

      }


        return vxShopCartIndexVO;
    }

    @Override
    public VxShopCartCheckoutVO checkedOut(VxShopcartCheckedOutBO vxShopcartCheckedOutBO, Integer id) {
        MarketOrderExample orderExample = new MarketOrderExample();
        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(orderExample);
        VxShopCartCheckoutVO checkoutVO = new VxShopCartCheckoutVO();
        if(vxShopcartCheckedOutBO.getCouponId()==0 || vxShopcartCheckedOutBO.getCouponId()==-1) {
            if (vxShopcartCheckedOutBO.getCartId() == 0) {
                for (MarketOrder marketOrder : marketOrders) {


                    checkoutVO.setFreightPrice(marketOrder.getFreightPrice());


                    checkoutVO.setGrouponPrice(marketOrder.getGrouponPrice());
                    checkoutVO.setGrouponRulesId(0);

                    int val = marketCartMapper.selectPriceByproductId(id);
                    BigDecimal resultGoodsTotalPrice = BigDecimal.valueOf(val);
                    //总金额
                    checkoutVO.setGoodsTotalPrice(resultGoodsTotalPrice);
                    //优惠券价格
                    //查到couponId
                    Integer[] conpinsID = couponUserMapper.selectByUserId(id);
                    //查到所有可用的优惠券

                    List<BigDecimal> resltId = (marketCouponMapper.selectByCouponID(conpinsID));
                    BigDecimal integer = resltId.get(0);

                    checkoutVO.setCouponPrice(integer);
                    BigDecimal add = resultGoodsTotalPrice.add(marketOrder.getFreightPrice());
                    BigDecimal subtract = add.subtract(marketOrder.getCouponPrice());
                    checkoutVO.setActualPrice(subtract);
                    checkoutVO.setOrderTotalPrice(subtract);

                    Integer count = couponUserMapper.selectCountByEndTime();
                    checkoutVO.setAvailableCouponLength(count);
                }


                int resultAddressId = marketAddressMapper.selectIdByUserId(id);
                checkoutVO.setAddressId(resultAddressId);


                checkoutVO.setUserCouponId(vxShopcartCheckedOutBO.getUserCouponId());
                checkoutVO.setCouponId(vxShopcartCheckedOutBO.getCouponId());
                checkoutVO.setCartId(0);

                MarketAddress marketAddress = marketAddressMapper.selectByUserId(id);
                checkoutVO.setCheckedAddress(marketAddress);
                List<MarketCart> marketCarts = marketCartMapper.selectByUserId(id);
                checkoutVO.setCheckedGoodsList(marketCarts);
            } else {
                for (MarketOrder marketOrder : marketOrders) {


                    checkoutVO.setFreightPrice(marketOrder.getFreightPrice());


                    checkoutVO.setGrouponPrice(marketOrder.getGrouponPrice());
                    checkoutVO.setGrouponRulesId(0);

                    //查到couponId
                    Integer[] conpinsID = couponUserMapper.selectByUserId(id);
                    //查到所有可用的优惠券

                    List<BigDecimal> resltId = (marketCouponMapper.selectByCouponID(conpinsID));
                    BigDecimal integer = resltId.get(0);

                    checkoutVO.setCouponPrice(integer);

                    BigDecimal resultGoodsTotalPrice = BigDecimal.valueOf(marketCartMapper.selectPriceCartId(vxShopcartCheckedOutBO.getCartId()));
                    checkoutVO.setGoodsTotalPrice(resultGoodsTotalPrice);
                    BigDecimal add = resultGoodsTotalPrice.add(marketOrder.getFreightPrice());
                    BigDecimal subtract = add.subtract(marketOrder.getCouponPrice());
                    checkoutVO.setActualPrice(subtract);
                    checkoutVO.setOrderTotalPrice(subtract);

                    Integer count = couponUserMapper.selectCountByEndTime();
                    checkoutVO.setAvailableCouponLength(count);


                }


                int resultAddressId = marketAddressMapper.selectIdByUserId(id);
                checkoutVO.setAddressId(resultAddressId);


                checkoutVO.setUserCouponId(vxShopcartCheckedOutBO.getUserCouponId());
                checkoutVO.setCouponId(vxShopcartCheckedOutBO.getCouponId());
                checkoutVO.setCartId(0);

                MarketAddress marketAddress = marketAddressMapper.selectByUserId(id);
                checkoutVO.setCheckedAddress(marketAddress);
                List<MarketCart> marketCarts = marketCartMapper.selectByCartId(vxShopcartCheckedOutBO.getCartId());
                checkoutVO.setCheckedGoodsList(marketCarts);


            }
        }else {
            if (vxShopcartCheckedOutBO.getCartId() == 0) {
                for (MarketOrder marketOrder : marketOrders) {


                    checkoutVO.setFreightPrice(marketOrder.getFreightPrice());


                    checkoutVO.setGrouponPrice(marketOrder.getGrouponPrice());
                    checkoutVO.setGrouponRulesId(0);

                    BigDecimal resultGoodsTotalPrice = BigDecimal.valueOf(marketCartMapper.selectPriceByproductId(id));
                    //总金额
                    checkoutVO.setGoodsTotalPrice(resultGoodsTotalPrice);
                    //优惠券价格
                    //查到couponId
                    Integer[] conpinsID = couponUserMapper.selectByUserId(id);
                    //查到所有可用的优惠券

                    List<BigDecimal> resltId = (marketCouponMapper.selectByCouponID(conpinsID));
                    //BigDecimal integer = resltId.get(0);
                    MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(vxShopcartCheckedOutBO.getCouponId());
                    //获取选中的优惠券
                    checkoutVO.setCouponPrice(marketCoupon.getDiscount());


                    BigDecimal add = resultGoodsTotalPrice.add(marketOrder.getFreightPrice());
                    BigDecimal subtract = add.subtract(marketOrder.getCouponPrice());
                    checkoutVO.setActualPrice(subtract);
                    checkoutVO.setOrderTotalPrice(subtract);

                    Integer count = couponUserMapper.selectCountByEndTime();
                    checkoutVO.setAvailableCouponLength(count);
                }


                int resultAddressId = marketAddressMapper.selectIdByUserId(id);
                checkoutVO.setAddressId(resultAddressId);


                checkoutVO.setUserCouponId(vxShopcartCheckedOutBO.getUserCouponId());
                checkoutVO.setCouponId(vxShopcartCheckedOutBO.getCouponId());
                checkoutVO.setCartId(0);

                MarketAddress marketAddress = marketAddressMapper.selectByUserId(id);
                checkoutVO.setCheckedAddress(marketAddress);
                List<MarketCart> marketCarts = marketCartMapper.selectByUserId(id);
                checkoutVO.setCheckedGoodsList(marketCarts);
            } else {
                for (MarketOrder marketOrder : marketOrders) {


                    checkoutVO.setFreightPrice(marketOrder.getFreightPrice());


                    checkoutVO.setGrouponPrice(marketOrder.getGrouponPrice());
                    checkoutVO.setGrouponRulesId(0);

                    //查到couponId
                    Integer[] conpinsID = couponUserMapper.selectByUserId(id);
                    //查到所有可用的优惠券

                    List<BigDecimal> resltId = (marketCouponMapper.selectByCouponID(conpinsID));
                    //BigDecimal integer = resltId.get(0);


                    MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(vxShopcartCheckedOutBO.getCouponId());
                    //获取选中的优惠券
                    checkoutVO.setCouponPrice(marketCoupon.getDiscount());


                    BigDecimal resultGoodsTotalPrice = BigDecimal.valueOf(marketCartMapper.selectPriceCartId(vxShopcartCheckedOutBO.getCartId()));
                    checkoutVO.setGoodsTotalPrice(resultGoodsTotalPrice);
                    BigDecimal add = resultGoodsTotalPrice.add(marketOrder.getFreightPrice());
                    BigDecimal subtract = add.subtract(marketOrder.getCouponPrice());
                    checkoutVO.setActualPrice(subtract);
                    checkoutVO.setOrderTotalPrice(subtract);

                    Integer count = couponUserMapper.selectCountByEndTime();
                    checkoutVO.setAvailableCouponLength(count);


                }


                int resultAddressId = marketAddressMapper.selectIdByUserId(id);
                checkoutVO.setAddressId(resultAddressId);


                checkoutVO.setUserCouponId(vxShopcartCheckedOutBO.getUserCouponId());
                checkoutVO.setCouponId(vxShopcartCheckedOutBO.getCouponId());
                checkoutVO.setCartId(0);

                MarketAddress marketAddress = marketAddressMapper.selectByUserId(id);
                checkoutVO.setCheckedAddress(marketAddress);
                List<MarketCart> marketCarts = marketCartMapper.selectByCartId(vxShopcartCheckedOutBO.getCartId());
                checkoutVO.setCheckedGoodsList(marketCarts);


            }
        }


        return checkoutVO;
    }

    @Override
    public int fastAdd(VxShopCartFastAddBO vxShopCartFastAddBO, Integer id) {
      Integer marketCart =   marketCartMapper.selectIdBygoodsIdAndProductId(vxShopCartFastAddBO.getGoodsId(),vxShopCartFastAddBO.getProductId());
        Integer id1 = 0;

      if(marketCart ==null){


          MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(vxShopCartFastAddBO.getGoodsId());
          MarketGoodsProduct marketGoodsProduct = marketGoodsProductMapper.selectByPrimaryKey(vxShopCartFastAddBO.getProductId());
          String picUrl = marketGoods.getPicUrl();
          Date addTime = marketGoods.getAddTime();
          Date updateTime = marketGoods.getUpdateTime();
          BigDecimal price = marketGoodsProduct.getPrice();

          String[] specifications = marketGoodsProduct.getSpecifications();
          String goodsSn = marketGoods.getGoodsSn();
          String name = marketGoods.getName();
          MarketCart marketCart1 = new MarketCart();
          marketCart1.setId(null);
          marketCart1.setUserId(id);
          marketCart1.setGoodsId(vxShopCartFastAddBO.getGoodsId());
          marketCart1.setGoodsSn(goodsSn);
          marketCart1.setGoodsName(name);
          marketCart1.setProductId(vxShopCartFastAddBO.getProductId());
          marketCart1.setPrice(price);
          marketCart1.setNumber(vxShopCartFastAddBO.getNumber());
          marketCart1.setSpecifications(String.valueOf(specifications));
          marketCart1.setChecked(false);
          marketCart1.setPicUrl(picUrl);
          marketCart1.setAddTime(addTime);
          marketCart1.setUpdateTime(updateTime);
          marketCart1.setDeleted(false);

          int result =  marketCartMapper.insertAllData(marketCart1);
         if(result!=0){
             //id1 =  marketCartMapper.selectByIdGoodId(vxShopCartFastAddBO.getGoodsId());
             id1 = marketCart1.getId();
         }




      }else {
          marketCartMapper.updateByIdNumber(vxShopCartFastAddBO.getNumber(),vxShopCartFastAddBO.getGoodsId());
         id1 =  marketCartMapper.selectIdByGoodsId(vxShopCartFastAddBO.getGoodsId());

      }


        return id1;
    }


}
